
#include <asm.h>

.global C_SYMBOL(AlphaOnlyBlend_8u_C4A)

C_SYMBOL(AlphaOnlyBlend_8u_C4A):

    STMFD	SP!, {R4-R12,LR}
Start:
    MOV	LR, R0

    LDR R11, [LR, #0x0]                 //pSrc
    LDR	R8,  [LR, #0x8]                 //SrcStep
    LDR	R10, [LR, #0x4]                //pDst
	LDR	R12, [LR, #0xC]                //DstStep

    LDR	R4, [LR, #0x10]                 //width
	LDR	R7, [LR, #0x14]                 //height

	LDR	R1, [LR, #0x18]                 //color

	LDRB	R3, [LR, #0x1C]
    ORR R3, R3, R3, LSL #16
    TMCRR   wR14, R3, R3

	MOV R1, R1, LSR #8
	TMCRR   wR13, R1, R1
	WUNPCKELUB wR13, wR13

	MOV LR, R4, LSL #2

    LDR     R4, = 0x00FF00FF
    TMCRR   wR15, R4, R4

    LDR     R4, = 0x00000008
    TMCR    wCGR3, R4

LoopLine:
	MOV	R3, LR
	MOV	R2, R10
	MOV	R0, R11
	B Alpha_8u_C4ARow
Next:

	ADD	R11, R11, R8
	ADD	R10, R10, R12
	SUBS	R7, R7,	#1
	BHI	LoopLine

End:

	LDMFD	SP!, {R4-R12,PC}

Alpha_8u_C4ARow:

    ADD	R6, R2,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R2
    CMP	R6, #0
    BEQ	_Aligned8_

_Process_One_:

    LDRB   R1, [R0], #1
    CMP	R6, R3
    WLDRW   wR6, [R2]

    MOVGT	R6, R3
    SUB	R3, R3,	R6

_Loop_One_:
//.long 0xe7fddefe

    SUBS	R6, R6,	#4
    TBCSTH  wR5, R1

    WMULUL  wR5, wR14, wR5

    WUNPCKELUB wR1, wR6
    WSRLHG    wR5, wR5, wCGR3
    WMULUL  wR0, wR13, wR5
    WSUBH   wR7,wR15,wR5
    WMULUL  wR1, wR1, wR7

    LDRGTB   R1, [R0], #1
    WADDH     wR0, wR0, wR1
    WLDRWGT   wR6, [R2, #4]
    WSRLHG    wR0, wR0, wCGR3

    WPACKHUS wR0, wR0, wR1

    //ADD R2,R2,#4
    WSTRW   wR0, [R2], #4

    BGT	_Loop_One_

    CMP	R3, #0
    BEQ     Next

_Aligned8_:
    SUBS	R3, R3,	#0x10
    ADDMI	R6, R3,	#0x10
    ADDMI	R3, R3,	#0x10
    BMI	_Process_One_
    AND	R4, R0,	#3
    CMP R4, #0
    BNE	_UnAligned8_

    WLDRW   wR0, [R0], #4
    WLDRD   wR4, [R2]

    WLDRD   wR5, [R2, #8]

_Loop_Aligned8_:

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    TEXTRMUB R1, wR0, #0
    WUNPCKELUB wR8, wR4
    TBCSTH  wR7, R1
    WMULUL  wR7, wR14, wR7

    TEXTRMUB R1, wR0, #1

    WSRLHG    wR7, wR7, wCGR3

    WMULUL  wR6, wR13, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7


    WUNPCKEHUB wR11, wR4
    TBCSTH   wR10, R1
    WMULUL  wR10, wR14, wR10
    WADDH     wR6, wR6, wR8

    WSRLHG  wR6, wR6, wCGR3
    WSRLHG    wR10, wR10, wCGR3
    WMULUL  wR9, wR13, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10



    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9
    WSTRD   wR6, [R2], #8

    TEXTRMUB R1, wR0, #2
    WUNPCKELUB wR8, wR5
    TBCSTH  wR7, R1
    WMULUL  wR7, wR14, wR7
    TEXTRMUB R1, wR0, #3
    WSRLHG    wR7, wR7, wCGR3
    WMULUL  wR6, wR13, wR7

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3



    TBCSTH  wR10, R1
    WMULUL  wR10, wR14, wR10
    WUNPCKEHUB wR11, wR5
    WSRLHG    wR10, wR10, wCGR3
    WMULUL  wR9, wR13, wR10

    WLDRWGE wR0, [R0], #4

    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10
//    WLDRDGE wR2, [R0], #8
    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3
    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]
    WSTRD wR6, [R2], #8

    BGE	_Loop_Aligned8_
    ADDS	R3, R3,	#0x10
    BEQ     Next
    MOVGT	R6, R3
    //SUBGT   R2, R2, #8
    BGT	_Process_One_

_UnAligned8_:
//    AND	R4, R0,	#7
//    BIC	R0, R0,	#7
//    TMCR    wCGR1, R4

    LDRB   R1, [R0], #1
    WLDRD   wR4, [R2]

    LDRB   R5, [R0], #1
    WLDRD   wR5, [R2, #8]

_Loop_UnAligned8_:
@.long 0xe7fddefe
    //TST	R4, #7
    //WLDRDNE wR12, [R0]

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    TBCSTH     wR7, R1
    WMULUL  wR7, wR14, wR7
    WUNPCKELUB wR8, wR4
    WSRLHG    wR7, wR7, wCGR3

    WMULUL  wR6, wR13, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    LDRB   R1, [R0], #1

    TBCSTH    wR10, R5
    WMULUL  wR10, wR14, wR10
    WADDH     wR6, wR6, wR8
    WUNPCKEHUB wR11, wR4
    WSRLHG  wR6, wR6, wCGR3
    WSRLHG    wR10, wR10, wCGR3

    LDRB   R5, [R0], #1

    WMULUL  wR9, wR13, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

//    WLDRDGE wR0, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9

    //ADD R2,R2,#8

    WSTRD   wR6, [R2], #8

    TBCSTH  wR7, R1
    WMULUL  wR7, wR14, wR7
    WUNPCKELUB wR8, wR5
    WSRLHG    wR7, wR7, wCGR3
    WMULUL  wR6, wR13, wR7

    LDRGEB   R1, [R0], #1

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3


    TBCSTH  wR10, R5
    WMULUL  wR10, wR14, wR10
    WUNPCKEHUB wR11, wR5
    WSRLHG    wR10, wR10, wCGR3
    WMULUL  wR9, wR13, wR10

    LDRGEB   R5, [R0], #1

    WSUBH   wR10, wR15, wR10
    WMULUL  wR11, wR11, wR10

//    WLDRDGE wR2, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3


    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]

    //ADD R2,R2,#8
    WSTRD   wR6, [R2], #8

    BGE	_Loop_UnAligned8_

    ADD	R0, R0,	R4
    ADDS	R3, R3,	#0x10
    BEQ     Next
    MOVGT	R6, R3
    BGT	_Process_One_
